const state = {
  checkedAll: false,
  orderList: []
}

const mutations = {
  SET_ORDER_LIST(state, { key, data }) {
    state.checkedAll = false
    state[key] = data.map(item => {
      return {
        checked: false,
        ...item
      }
    })
  },
  SELECT_ALL_CHANGE(state, { key, checked }) {
    state.checkedAll = checked
    state[key] = state[key]?.map(item => {
      item.checked = checked
      return item
    })
  },
  SELECT_ITEM_CHANGE(state, { key, checked, id }) {
    state[key] = state[key]?.map(item => {
      if (item.id === id) {
        item.checked = checked
      }
      return item
    })
  },
  SELECT_CHECKED_ALL_CHANGE(state, { key }) {
    const checked = state[key]?.every(item => item.checked)
    state.checkedAll = checked
  }
}

const actions = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
